package com.bookstore.MapperDao;

import com.bookstore.bean.TradeItem;
import utils.MybatisSessionFactory;


import java.util.*;

/**
 * Created by Admin on 2016/8/12.
 */
public class TradeItemMapImp implements TradeItemMap {
    private String str = "com.bookstore.mapper.TradeItemMapper.";

    /**
     * 批量保存TradeItem对象
     *
     * @param items
     */
    public void batchSave(Collection<TradeItem> items) {
        String statement = str + "batchSave";
        Map<String, Object> map1 = new HashMap<>();
        List<Map<String, Integer>> list = new ArrayList<>();
        List<TradeItem> tradeItems = new ArrayList<>(items);
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < items.size(); i++) {
            map.put("bookId", tradeItems.get(i).getBookId());
            map.put("quantity", tradeItems.get(i).getQuantity());
            map.put("tradeId", tradeItems.get(i).getTradeId());
        }
        list.add(map);
        map1.put("list", list);
        MybatisSessionFactory.getSession().insert(statement, map1);

    }

    /**
     * tradeId 获取和其关联的TradeItem 的集合
     *
     * @param tradeId
     * @return
     */
    public Set<TradeItem> getTradeItemsWithTradeId(Integer tradeId) {
        String statement = str + "getTradeItemsWithTradeId";
        //List<TradeItem> list=  MybatisSessionFactory.getSession().selectList(statement,tradeId);
        Set<TradeItem> set = new HashSet<>(MybatisSessionFactory.getSession().selectList(statement, tradeId));
        return set;
    }
}
